<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../layuimini-2/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../layuimini-2/css/layuimini.css" media="all">


</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" id="like">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">租金</label>
                            <div class="layui-input-inline">
                                <input type="text" name="rent" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">会议人</label>
                            <div class="layui-input-inline">
                                <input type="text" name="conName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">面积</label>
                            <div class="layui-input-inline">
                                <input type="text" name="area" autocomplete="off" class="layui-input">
                            </div>
                        </div>
<!--                        <div class="layui-inline">-->
<!--                            <label class="layui-form-label">主题</label>-->
<!--                            <div class="layui-input-inline">-->
<!--                                <input type="text" name="conSubject" autocomplete="off" class="layui-input">-->
<!--                            </div>-->
<!--                        </div>-->
                        <div class="layui-inline">
                            <label class="layui-form-label">主题</label>
                            <div class="layui-input-block">
                                <select name="conSubject" id="conSubject" lay-verify="required" >
                                    <option value="0">请选择</option>
                                </select>
                            </div>
                        </div>

                        <div class="layui-inline">

                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                            <button type="submit" class="layui-btn layui-btn-primary"  ><i class="layui-icon"></i> 全部搜索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>


        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="deleteBatch"> 批量删除 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="bar"> 柱状图 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="pie"> 饼状图 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script src="../../../layuimini-2/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>

<script>
    var $ ;
    var form;
    var table;
    var layer;
    layui.use(['form','table',"jquery"], function () {
         $ = layui.jquery;
         form = layui.form;
         table = layui.table;


        //下拉选择框动态赋值
        // $.ajax({
        //     url:"/list/conSubject.do",//controller请求地址
        //     dataType:"json",
        //     type:"get",
        //     success:function (data) { //注意后端代码返回数据key值必须同名 为data
        //         console.log(data)
        //         //使用循环遍历，给下拉列表赋值
        //         $.each(data.data,function (index,value) {
        //             $("#List").append(new Option(value.conSubject,value.id))//对应映射字段名 第一个为显示的值  第二个为value值
        //         });
        //         layui.form.render("select")//重新渲染 固定写法
        //     }
        // })
        //动态加载下拉列表数据
        //打开页面 动态查询下拉列表数据
        $.ajax({
            url: '/conference/select.do',
            dataType: 'json',
            type: 'get',
            success: function (data) {
                console.log(data);//下面会提到这个data是什么值
                //使用循环遍历，给下拉列表赋值
                $.each(data.data, function (index, value) {//传入数组,不是对象
                    console.log(value);
                    $('#conSubject').append(new Option(value, value));// 下拉菜单里添加元素

                });
                layui.form.render("select");//重新渲染 固定写法

            },
            end: function () {
                table.reload("like")
            },
            async: false//不异步,异步锁

        })

        //渲染表格
        tableRender();

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (obj) {
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: obj.field
            }, 'data');

            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                layer.open({
                    title: '添加用户',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/view/hall/conference/conferenceAdd.html',
                    end: function () {
                        table.reload("currentTableId")
                    }
                });

            } else if (obj.event === 'deleteBatch') {  // 监听批量删除操作
                var checkStatus = table.checkStatus("currentTableId");
                if (checkStatus.data.length !=0) {
                    var arr = [];
                    for (var i=0;i < checkStatus.data.length;i++) {
                        arr.push(checkStatus.data[i].id);
                    }
                    $.ajax({
                        url: "/conference/batch.do",
                        data:{ids:arr},
                        type:"get",
                        traditional:true,
                        success: function (ret) {
                            if (ret.code==0) {
                                layer.msg("删除成功!")
                                table.reload("currentTableId");

                            }
                        },
                        error:function () {
                            layer.msg("服务器忙!");
                        }
                    })
                } else {
                    layer.alert("请选择需要删除的数据",{icon:0,title:'提示'} )
                }
            }else if (obj.event === 'bar') {  // 柱状图操作
                layer.open({
                    title: '报告厅面积大小饼状图',
                    shade: 0.2,
                    type: 2,
                    shadeClose: true,   // 点别的地方 是否关闭当前 layer    true收关闭   false 不关闭
                    area: ['80%', '80%'],
                    fixed: true, //不固定
                    maxmin: true,
                    content: '/view/hall/conference/conferenceBar.html',
                    end: function (index) {

                    }
                });
            }else if (obj.event === 'pie') {  // 饼状图操作
                layer.open({
                    title: '报告厅面积大小饼状图',
                    shade: 0.2,
                    type: 2,
                    shadeClose: true,   // 点别的地方 是否关闭当前 layer    true收关闭   false 不关闭
                    area: ['80%', '100%'],
                    fixed: true, //不固定
                    maxmin: true,
                    content: '/view/hall/conference/conferencePie.html',
                    end: function (index) {

                    }
                });
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            // console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '编辑用户',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/view/hall/conference/conferenceEdit.html',
                    success: function (layero, index) {//弹出层open方法
                        let body = layer.getChildFrame("body", index);
                        let data = obj.data;
                        // console.log(houseData.id)
                        body.find("input[name=id]").val(data.id)
                        body.find("input[name=area]").val(data.area)
                        body.find("input[name=num]").val(data.num)
                        let date = new Date(data.conTime);
                        let year = date.getFullYear();
                        let month = date.getMonth() + 1;
                        if (month < 10) {
                            month = "0" + month;
                        }
                        let day = date.getDate();
                        if (day < 10) {
                            day = "0" + day;
                        }
                        let addTime1 = year + "-" + month + "-" + day;
                        body.find("input[name=conTime]").val(addTime1)

                        let date1 = new Date(data.endTime);
                        let year1 = date1.getFullYear();
                        let month1 = date1.getMonth() + 1;
                        if (month1 < 10) {
                            month1 = "0" + month1;
                        }
                        let day1 = date.getDate();
                        if (day1 < 10) {
                            day1 = "0" + day1;
                        }
                        let updateTime3 = year1 + "-" + month1 + "-" + day1;
                        body.find("input[name=endTime]").val(updateTime3)

                        body.find("input[name=rent]").val(data.rent)
                        body.find("input[name=conName]").val(data.conName)
                        body.find("input[name=tel]").val(data.tel)
                        body.find("input[name=conSubject]").val(data.conSubject)
                    },
                    end: function () {
                        table.reload("currentTableId")
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm("确认删除", {icon: 3, title: "提示"}, function (index) {
                    //layer.msg("删除成功")
                    //发送请求
                    $.get("/conference/delete.do", {id: obj.data.id}, function (ret) {
                        if (ret.code == 0) {
                            layer.msg("删除成功")
                        } else {
                            layer.msg("删除失败")
                        }
                    });
                    layer.close(index);
                    table.reload("currentTableId")
                }, function (index) {
                    layer.msg("取消删除")
                    layer.close(index)
                });
            }
        });


        function tableRender(obj) {
        table.render({
            elem: '#currentTableId',
            url: '/conference/list.do',
            toolbar: '#toolbarDemo',
            page: true,
            limit: 6,
            limits: [6, 12, 18, 24],
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id', width: 80, title: 'ID', sort: true},
                {field: 'area', width: 80, title: '面积'},
                {field: 'num', title: '人数', width: 70},
                {field: 'conTime', width: 120, title: '预定时间', sort: true, templet: function (obj) {
                        let date = new Date(obj.conTime);
                        let year = date.getFullYear();
                        let month = date.getMonth() + 1;
                        if (month < 10) {
                            month = "0" + month
                        }
                        let day = date.getDate();
                        if (day < 10) {
                            day = "0" + day
                        }
                        return year + "-" + month + "-" + day
                    }},
                {field: 'endTime', width: 120, title: '结束时间', sort: true, templet: function (obj) {
                        let date = new Date(obj.endTime);
                        let year = date.getFullYear();
                        let month = date.getMonth() + 1;
                        if (month < 10) {
                            month = "0" + month
                        }
                        let day = date.getDate();
                        if (day < 10) {
                            day = "0" + day
                        }
                        return year + "-" + month + "-" + day
                    }},
                {field: 'rent', width: 80, title: '租金'},
                {field: 'conName', width: 135, title: '预定人', sort: true},
                {field: 'conSubject', width: 135, title: '主题', sort: true},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            skin: 'line'
        });
    }
    });


</script>

</body>
</html>